import { reactive, toRefs, computed } from 'vue';
import { changeStatus, getAllUsers } from '@/api/user';
import { TableUser } from '@/api/types';
import { deStatusWord, formatUsers } from '@/utils/formatDataFromServer';
import { confirm, error, showDescription, success } from '@/utils/util';

export default () => {
  const state = reactive<{
    search: string;
    sort: string;
    tableData: TableUser[];
  }>({
    tableData: [],
    search: '',
    sort: '',
  });

  /**
   * 下拉菜单选项
   * @param command
   */
  const selectWord = (command: string) => {
    state.sort = command;
  };

  /**
   * tabel的数据
   */
  const data = computed(() => {
    return filterData();
  });

  /**
   * 获取所有的用户
   */
  const getUsers = async () => {
    const { code, data } = await getAllUsers();
    console.log('总人数: ', data.length);
    // let str = '';
    // data.filter(item => item.direction === '前端' && item.openid.startsWith('o')).forEach(item => str += `${item.name + '|' + (item.date || '无')}\r\n`);
    // data.filter(item => item.direction === '后台' && item.openid.startsWith('o')).forEach(item => str += `${item.name + '|' + (item.date || '无')}\r\n`);
    // console.log(str);

    console.log('前端: ', data.filter(item => item.direction === '前端').length );
    console.log('后台: ', data.filter(item => item.direction === '后台').length );

    if (code == '1') {
      state.tableData = formatUsers(data);
    }
  };



  /**
   * 通过
   * @param openid 
   * @param status 
   */
  const pass = async (row: TableUser) => {
    let { openid, status, name } = row;
    // 获得文字的状态转为数组的状态
    status = deStatusWord(status);

    // 只有面试和考核的状态才能更改信息
    if (status == '2' || status == '3') {
      // 1. 弹出确认消息
      const isConfirm = await confirm('success', '提示', `确定让${name}通过${status == '2' ? '面试' : '考核'}吗`);
      if (isConfirm) { // 确定通过
        if (status == '2') {
          // 面试通过
          await changeStatusFromServer(openid, '3', '通过面试失败');
        } else if (status == '3') {
          // 考核通过
          await changeStatusFromServer(openid, '4', '通过考核失败');
        }
      }
    } else {
      error('不可操作')
    }
  }

  /**
   * 不通过
   * @param openid
   * @param status
   */
  const refuse = async (row: TableUser) => {
    let { openid, status, name } = row;
    // 获得文字的状态转为数组的状态
    status = deStatusWord(status);
    // 只有面试和考核的状态才能更改信息
    if (status == '2' || status == '3') {
      // 1. 弹出确认消息
      const isConfirm = await confirm(
        'error',
        '提示',
        `确定让${name}不通过${status == '2' ? '面试' : '考核'}吗`,
      );
      if (isConfirm) {
        // 确定
        if (status == '2') {
          // 面试不通过
          await changeStatusFromServer(openid, '-2', '通过面试失败');
        } else if (status == '3') {
          // 考核不通过
          await changeStatusFromServer(openid, '-3', '通过考核失败');
        }
      }
    } else {
      error('不可操作');
    }
  };

  /**
   * 更改面试和考核的状态
   * @param status
   * @param errorMsg
   */
  const changeStatusFromServer = async (
    openid: string,
    status: string,
    errorMsg: string,
  ) => {
    const { code, data } = await changeStatus(openid, status);
    if (code == '1') {
      // 1. 重新获取用户信息
      getUsers();
      // 2. 弹出消息成功
      success(data);
    } else {
      error(errorMsg);
    }
  };

  /**
   * 过滤数据
   */
  const filterData = () => {
    // 先过滤sort的，再过滤搜索的
    return state.tableData
      .filter((data, i) => {
        return (
          data.name.toLowerCase().includes(state.sort.toLowerCase()) ||
          data.status.toLowerCase().includes(state.sort.toLowerCase()) ||
          data.direction.toLowerCase().includes(state.sort.toLowerCase()) ||
          data.duration.toLowerCase().includes(state.sort.toLowerCase()) 
        );
      })
      .filter((data) => {
        return (
          data.name.toLowerCase().includes(state.search.toLowerCase()) ||
          data.status.toLowerCase().includes(state.search.toLowerCase()) ||
          data.direction.toLowerCase().includes(state.search.toLowerCase())
        );
      });
  };

  /**
     * 根据不同的状态加上不同的颜色
     * @param rowMsg 
     */
  const ccn = (rowMsg: { row: TableUser, column: any, rowIndex: number, columnIndex: number }) => {
    const { row, columnIndex } = rowMsg;
    if (columnIndex === 5) { // 得到
      let { status } = row;
      // 获得文字的状态转为数组的状态
      status = deStatusWord(status);
      if (status == '2' || status == '3' || status == '4') {
        return 'gr';
      } else if (status == '-2' || status == '-3') {
        return 'rd';
      }
    }
  }
  // 显示简介信息
  const show = (row: TableUser) => {
    showDescription(row.intro);
  }
  return {
    ...toRefs(state),
    selectWord,
    data,
    getUsers,
    pass,
    refuse,
    ccn,
    show
  };
}
